package org.example.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;
@Component
public class EmailService {
    @Autowired
    JavaMailSender javaMailSender;

    public void sendMail(String s,String s1) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
        mimeMessageHelper.setText("啊吧啊吧");
        mimeMessageHelper.setFrom(GlobalConts.EMAIL_USERNAME);
        mimeMessageHelper.setTo(s);
        mimeMessageHelper.setSubject("主题");

        if(s1!=null && !s1.isEmpty()){
            File file = new File(s1);
            mimeMessageHelper.addAttachment(file.getName(),file);
        }

        mimeMessageHelper.setSentDate(new Date());

        javaMailSender.send(mimeMessage);
    }
}
